Passed
Push — master ( 6a58a9...338c4b )
by Dongxin
02:20
created

index.js ➔ ???   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 16
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 21
rs 9.6
1
/* global hexo, __dirname */
2
3
'use strict';
4
5
const fs = require('hexo-fs');
6
const path = require('path');
7
const CryptoJS = require('crypto-js');
8
const log = require('hexo-log')({
9
  'debug': false,
10
  'silent': false,
11
});
12
13
hexo.extend.filter.register('after_post_render', function encrypt (data) {
14
15
  // Close the encrypt function
16
  if (!('encrypt' in hexo.config && hexo.config.encrypt && 'enable' in hexo.config.encrypt && hexo.config.encrypt.enable)) {
17
18
    return data;
19
20
  }
21
  if (!('default_template' in hexo.config.encrypt && hexo.config.encrypt.default_template)) { // No such template
22
23
    hexo.config.encrypt.default_template = fs.readFileSync(path.resolve(__dirname, './template.html'));
24
25
  }
26
  if (!('default_abstract' in hexo.config.encrypt && hexo.config.encrypt.default_abstract)) { // No read more info
27
28
    hexo.config.encrypt.default_abstract = 'The article has been encrypted, please enter your password to view.<br>';
29
30
  }
31
  if (!('default_message' in hexo.config.encrypt && hexo.config.encrypt.default_message)) { // No message
32
33
    hexo.config.encrypt.default_message = 'Please enter the password to read the blog.';
34
35
  }
36
  if (!('default_decryption_error' in hexo.config.encrypt && hexo.config.encrypt.default_decryption_error)) { // Wrong password
37
38
    hexo.config.encrypt.default_decryption_error = 'Incorrect Password!';
39
40
  }
41
  if (!('default_no_content_error' in hexo.config.encrypt && hexo.config.encrypt.default_no_content_error)) { // No content
42
43
    hexo.config.encrypt.default_no_content_error = 'No content to display!';
44
45
  }
46
47
  if ('password' in data && data.password) {
48
49
    // Use the blog's config first
50
    log.info(`Encrypted the blog: ${ data.title.trim() }`);
51
52
    // Store the origin data
53
    data.origin = data.content;
54
    data.encrypt = true;
55
56
    if (!('abstract' in data && data.abstract)) {
57
58
      data.abstract = hexo.config.encrypt.default_abstract;
59
60
    }
61
    if (!('template' in data && data.template)) {
62
63
      data.template = hexo.config.encrypt.default_template;
64
65
    }
66
    if (!('message' in data && data.message)) {
67
68
      data.message = hexo.config.encrypt.default_message;
69
70
    }
71
    if (!('decryptionError' in data && data.decryptionError)) {
72
73
      data.decryptionError = hexo.config.encrypt.default_decryption_error;
74
75
    }
76
    if (!('noContentError' in data && data.noContentError)) {
77
78
      data.noContentError = hexo.config.encrypt.default_no_content_error;
79
80
    }
81
82
    if (data.content.trim() === '') {
83
84
      log.warn('Warning: Your blog has no content, it may cause error when decrypting.');
85
86
    }
87
88
    data.content = escape(data.content);
89
    data.content = CryptoJS.enc.Utf8.parse(data.content);
90
    data.content = CryptoJS.enc.Base64.stringify(data.content);
91
    data.content = CryptoJS.AES.encrypt(data.content, String(data.password)).toString();
92
93
    data.template = data.template.replace('{{content}}', data.content);
94
    data.template = data.template.replace('{{message}}', data.message);
95
    data.template = data.template.replace('{{message}}', data.message);
96
    data.template = data.template.replace('{{decryptionError}}', data.decryptionError);
97
    data.template = data.template.replace('{{noContentError}}', data.noContentError);
98
99
    data.content = data.template;
100
    data.content += `<script src="${hexo.config.root}lib/crypto-js.js"></script>`;
101
    data.content += `<script src="${hexo.config.root}lib/blog-encrypt.js"></script>`;
102
    data.content += `<link href="${hexo.config.root}css/blog-encrypt.css" rel="stylesheet" type="text/css">`;
103
104
    data.more = data.abstract;
105
    data.excerpt = data.more;
106
107
  }
108
109
  return data;
110
111
});
112
113
hexo.extend.generator.register('blog-encrypt', () => [
114
  {
115
    'data': () => fs.createReadStream(path.resolve(path.dirname(require.resolve('crypto-js')), 'crypto-js.js')),
116
    'path': 'lib/crypto-js.js',
117
  }, {
118
    'data': function () {
119
120
      const Readable = require('stream').Readable;
121
      const stream = new Readable();
122
      stream.push(fs.readFileSync(path.resolve(__dirname, 'lib/blog-encrypt.js'))
123
        .replace('{callback}', hexo.config.encrypt && hexo.config.encrypt.enable && hexo.config.encrypt.callback ? hexo.config.encrypt.callback : ''));
124
      stream.push(null); // Indicates the end of the stream
125
      return stream;
126
127
    },
128
    'path': 'lib/blog-encrypt.js',
129
  }, {
130
    'data': () => fs.createReadStream(path.resolve(__dirname, 'lib/blog-encrypt.css')),
131
    'path': 'css/blog-encrypt.css',
132
  },
133
]);
134